import xurl from '../../../x/xurl'
import Vue from "vue";
let urlHeader = Vue.prototype.NW_API_API_LOCALE_VERSION;
export default {
    /*successCallBack关注成功的回调，failMsgCallBack 失败的hui,urlHeader url地址前缀，userId 点击关注的用户Id,关注的类型0是用户，1是商家，concernKey  关注的Key/相应的id*/
    doAttention(callback,userId,concernType,concernKey){
        if(!Vue.prototype.userId){
            this.$router.push('/to/login');
            return;
        }
        let url = `${urlHeader}/concern/user/${userId}/do?concernType=${concernType}&&concernKey=${concernKey}`;
        xurl.put(url).then((data)=>{
            if(data.success){
                if(callback && callback.successCallBack){
                    callback.successCallBack();
                }
            }else if(data.errMsg){
                if(callback && callback.failMsgCallBack){
                    callback.failMsgCallBack(data.errMsg)
                }
            }else{
                if(callback && callback.failMsgCallBack){
                    callback.failMsgCallBack('关注失败')
                }
            }
        });
    },
    /*successCallBack关注成功的回调，urlHeader url地址前缀，userId 点击关注的用户Id,关注的类型0是用户，1是商家，concernKey  关注的Key/相应的id*/
    cancleAttention(callback,userId,concernType,concernKey){
        let url = `${urlHeader}/concern/user/${userId}/cancel?concernType=${concernType}&&concernKey=${concernKey}`;
        xurl.put(url).then((data)=>{
            if(data.success){
                if(callback && callback.successCallBack){
                    callback.successCallBack();
                }
            }else if(data.errMsg){
                if(callback && callback.failMsgCallBack){
                    callback.failMsgCallBack(data.errMsg)
                }
            }else{
                if(callback && callback.failMsgCallBack){
                    callback.failMsgCallBack('取消关注失败')
                }
            }
        });
    },
    /*
    * obj={
    *   isLike:false//是否点赞
    *   mainkey:1,//是否有主评论id //不需要时不传
    *   subKey:1,//是否有子评论id //不需要时不传
    *   likeKey:0,//点赞对象的id
    *   likeType:0,0:主评论  1：子评论 2: 店铺实景 3：店铺商品 4：店铺活动 5： 店铺品牌 10:多媒体信息
    * }
    * queryMsg:{//调用接口需要的参数
    *   userId:0,
    * },
    *callback{
    *   successCallBack:function(){};
    * }//请求接口之后的回调函数；//包括成功和失败
    * */
    isLike(obj,queryMsg,callback){
        // likeType
        ///like/user/{userId}/cancel
        // commentType
        // http://192.168.1.163:7280/nw_api/api/zh/v1.00/like/user/2004/do?likeType=2&&likeKey=3677
        //http://192.168.1.163:7280/nw_api/api/zh/v1.00/like/user/2004/do?likeType=2&&likeKey=31
        let likeType=-1;
        /*子评论点赞*/
        if(obj && Object.keys(obj).includes('mainkey') ){
            if( Object.keys(obj).includes('subKey') ){
                likeType = 1;
            }else {
                likeType = 0;
            }
        }else if(obj && obj.likeType){
            likeType = obj.likeType;
        }else{
            likeType = 10;
        }
        let url = '';
        if(obj.isLike){
            console.log(queryMsg)
            url=`${urlHeader}/like/user/${queryMsg.userId}/do?likeType=${likeType}&&likeKey=${obj.likeKey}`
        }else{
            url=`${urlHeader}/like/user/${queryMsg.userId}/cancel?likeType=${likeType}&&likeKey=${obj.likeKey}`
        }
        xurl.put(url).then((data)=>{
            if(data.success){
                if(callback && callback.successCallBack){
                    callback.successCallBack();
                }
            }else{
                console.log("点赞失败")
            }
        })
    },
    /*评论*/
    /*
    *
    * obj={
    *
    *
    * }
    *
    *
    * */
    doComment(obj,queryMsg,paramsMsg,callback){
        let url = '';
        let paramas={};
        /*主评论*/
        if(obj.belongType < 0){
            url=`${urlHeader}/comment/user/${queryMsg.userId}/add/main`;
            paramas={
                commentType:paramsMsg.commentType,
                commentKey:paramsMsg.commentKey,
                content:obj.content,
            }
        }else{
            /*子评论*/
            url=`${urlHeader}/comment/user/${queryMsg.userId}/add/sub`;
            paramas={
                commentMainId:obj.mainId,
                commentSubId:parseInt(obj.subId) > 0 ? obj.subId : null,
                content:obj.content,
            };
        }
        xurl.postDataJsonLoading(url,paramas).then((data)=>{
            if(data.success){
                if(callback && callback.successCallBack){
                    callback.successCallBack();
                }
            }else if(data.errMsg){
                if(callback && callback.failMsgCallBack){
                    callback.failMsgCallBack(data.errMsg)
                }
            }else{
                if(callback && callback.failMsgCallBack){
                    callback.failMsgCallBack('评论失败')
                }
            }
        })
    },
}